package com.yunyao.framework.service.impl;

import com.yunyao.dao.model.SysMenu;
import com.yunyao.dao.model.User;
import com.yunyao.dao.service.system.ISysMenuService;
import com.yunyao.dao.service.system.ISysRoleService;
import com.yunyao.dao.service.system.IUserService;
import com.yunyao.framework.service.system.MenuService;
import com.yunyao.framework.vo.system.MenuVO;
import com.yunyao.framework.vo.system.Meta;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenyejian
 * @date 2024-07-16
 */
@Slf4j
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private ISysMenuService iSysMenuService;
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private IUserService iUserService;

    @Override
    public List<MenuVO> getMenuListByUserId(String userId) {
        User user = iUserService.getById(userId);
        if (user.getRoleId().contains("-1")){
            return convert(iSysMenuService.getMenuListByRoleIds(""));
        }
        return convert(iSysMenuService.getMenuListByRoleIds(user.getRoleId()));
    }

    @Override
    public List<MenuVO> getMenuListByRoleIds(String roleId) {
        return convert(iSysMenuService.getMenuListByRoleIds(roleId));
    }

    private List<MenuVO> convert(List<SysMenu> menuList) {
        return menuList.stream().map((menu)->{
            MenuVO menuVo = new MenuVO();
            BeanUtils.copyProperties(menu, menuVo);
            menuVo.setMeta(new Meta(menu.getName(),menu.getSource(), menu.getSort(),menu.getShowed()));
            menuVo.setName(menu.getCode());
            return menuVo;
        }).collect(Collectors.toList());
    }
}
